<?php
class MainAction extends BaseAction {
    public function index(){

		$dataModel = D('Data');

		import('@.Widget.Chart');
		$chart = new Chart;

		while($n < 12){
			$devices[] = '设备' . ++$n;
		}
		$this->devices = $devices;
		$this->devicetype = $devicetype = $dataModel->devicetype;
		$this->interval = array(
			300				=>	'5分钟',
			600				=>	'15分钟',
			1800			=>	'半小时',
			3600			=>	'一小时',
			3600 * 4		=>	'4小时',
			3600 * 6		=>	'6小时',
			3600 * 8		=>	'8小时',
			3600 * 12		=>	'12小时',
			3600 * 24		=>	'一天',
			3600 * 24 * 7	=>	'一周',
			3600 * 24 * 30	=>	'一个月',
		);

		//设备
		$device = max(0, intval($_GET['device'])) + 1;
		$device = $device > 12 ? 1 : $device;

		//间隔时间
		$interval = max(60, intval($_GET['interval']));

		//时间区间
		$starttime = $_GET['starttime'] ? strtotime($_GET['starttime']) : time() - 36000;
		$starttime = $starttime ? $starttime : time() - 36000;

		$endtime = $_GET['endtime'] ? strtotime($_GET['endtime']) : $starttime + 3600;
		$endtime = $endtime ? $endtime : $starttime + 3600 * 24 * 7;

		$this->starttime = date('Y-m-d H:i:s', $starttime);
		$this->endtime = date('Y-m-d H:i:s', $endtime);

		//图表类型
		$datatype = intval($_GET['devicetype']);
		$unit = $datatype === DataModel::TYPE_WIND ? 'm/s' : 'mm';

		$dataModel = $dataModel->setInterval($interval)
			->setName('设备1')
			->setDataType($datatype)
			->setTimeRange($starttime, $endtime)
			->setDevice($device);

		$this->chart = $chart->setTitle($devicetype[$datatype] . '监测')
			->setSubTitle(date('Y-m-d H:i', $starttime) . '~' . date('Y-m-d H:i', $endtime))
			->setLabel(sprintf('%s 单位%s', $devicetype[$datatype], $unit))
			->setDataSource($dataModel)
			->getChart();

		$this->display();
    }

	public function config(){
		dp(c());
	}

	public function log(){
		$this->currentMenu = __FUNCTION__;

		$log = D('log');
		$page = max(1, intval($_GET['p']));
		$limit = 20;

		$this->data = $log->page($page)
			->limit($limit)
			->order('id DESC')
			->select();

		$total = $log->count();
		import('ORG.Util.Page');
		$this->pager = new page($total, $limit);

		$this->display();
	}

	public function buildTestData(){
		D('Data')->buildTestData();
	}

	public function importdata(){
		set_time_limit(0);
		ini_set('memory_limit', '128M');
		$model = D('Datasource');
		$items = $model->getSource();

		$dataModel = D('Data');
		$logModel = D('Log');
		$res = false;
		if(empty($items)){
			$msg = '暂时没有发现数据。';
		}else{
			foreach ($items as $item) {
				$res = $dataModel->parseFile($item);
				if(!$res){
					$log = '导入文件:' . $item . ', 发送错误。' . $dataModel->getError();
				}else{
					$log = '成功导入文件:' . $item . '。';
				}
				$logModel->log($log);
				$msg .= $log . (PHP_SAPI === 'cli' ? PHP_EOL : '<br />');
			}
			$res = true;
		}

		//导入图片
		$Photographic = D('Photographic');
		$Photographic->addItems($Photographic->getItems());

		if(PHP_SAPI === 'cli'){
			if($res === false){
				$logModel->log($msg);
			}
			die($msg);
		}

		$respond = array(
			'suc'	=>	$res,
			'msg'	=>	$msg,
			'data'	=>	array()
		);
		exit(json_encode($respond));
	}
	public function dev(){

		//导入图片
		$Photographic = D('Photographic');
		$Photographic->addItems($Photographic->getItems());
	}
}